# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
_luaversions="5.1 5.2 5.3"
pkgname=lua-psl
pkgver=0.3
pkgrel=0
pkgdesc="Lua binding to libpsl"
url="https://github.com/daurnimator/lua-psl"
arch="all"
license="MIT"
makedepends="libpsl-dev luarocks"
for _i in $_luaversions; do
	makedepends="$makedepends lua$_i-dev"
	subpackages="$subpackages lua$_i-psl:_package"
done
source="$pkgname-$pkgver.tar.gz::https://github.com/daurnimator/lua-psl/archive/v$pkgver.tar.gz"
builddir="$srcdir/lua-psl-$pkgver"

build() {
	cd "$builddir"

	local lver; for lver in $_luaversions; do
		msg "Building for Lua $lver"
		mkdir -p $lver
		# no makefile, only rocksfile so compile the single .c by hand
		echo " [CC] $lver/psl.so"
		${CC:-gcc} $(pkg-config --cflags libpsl lua$lver) -shared -o $lver/psl.so psl/psl.c
	done
}

package() {
	mkdir -p "$pkgdir"
	local lver; for lver in $_luaversions; do
		install -Dm755 $lver/psl.so "$pkgdir"/usr/lib/lua/$lver/psl.so
	done
}

_package() {
	local lver=${subpkgname:3:3}
	pkgdesc="$pkgdesc - for Lua $lver"
	depends="lua$lver"
	install_if="lua$lver $pkgname=$pkgver-r$pkgrel"
	local rockdir="$subpkgdir/usr/lib/luarocks/rocks-$lver/$pkgname/$pkgver-0"
	mkdir -p "$rockdir" "$subpkgdir"/usr/lib/lua/
	mv "$pkgdir"/usr/lib/lua/$lver "$subpkgdir"/usr/lib/lua/
	echo 'rock_manifest = {}' > "$rockdir"/rock_manifest
}

sha512sums="b77806f6deb4c668a7ce97583b41297494ef91a05a0f44041d17b52ef6542180f332169577195159bba554a0909fed697bd5f6e5e4de9174273b54739554f79f  lua-psl-0.3.tar.gz"
